package de.core.gameplay.actor;

import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;

public abstract class AbstractMovingActor extends AbstractActor {

    private final double speed;


    protected AbstractMovingActor(BufferedImage aImage, double aSpeed, int aX, int aY) {
        super(aImage, aX, aY);
        speed = aSpeed;
    }

    public double getSpeed() {
        return speed;
    }

    @Override
    public void updateGameState() {
        super.updateGameState();
        setLocation(getNextLocation(1.0f));
    }

    protected Point2D getNextLocation(float aInterpolation) {
        double velocityX = Math.cos(Math.toRadians(getRotation())) * getSpeed() * aInterpolation;
        double velocityY = Math.sin(Math.toRadians(getRotation())) * getSpeed() * aInterpolation;

        return new Point2D.Double(getLocation().getX() + velocityX,
                                  getLocation().getY() + velocityY);
    }

    @Override
    protected Point2D getImageRenderLocation(float aInterpolation) {
        Point2D locationAtRenderTime = getNextLocation(aInterpolation);
        double centerX = locationAtRenderTime.getX() - getImage().getWidth(null) / 2;
        double centerY = locationAtRenderTime.getY() - getImage().getHeight(null) / 2;
        return new Point2D.Double(centerX, centerY);
    }
}
